<?php

namespace app\index\controller;

use app\admin\model\Question as QuestionModel;
use think\Controller;
use think\Image;
use think\Request;
use app\admin\model\User as UserModel;
use think\Session;

class User extends Common
{
    protected $request;
    protected $user;
    protected $question;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = Request::instance();
        $this->user = new UserModel();
        $this->question = new QuestionModel();
    }

    public function collection()
    {
        $user_id = input('user_id', '');
        if (!$user_id) {
            $this->error('请先登录', 'login/index');
        }
        $questions = $this->question->getAuuu(['a.user_id' =>$user_id,'q_del'=>0,'audit'=>1]);
        $user = $this->user->getOne(['user_id' => $user_id]);
        $this->assign('questions', $questions);
        $this->assign('user', $user);
        return $this->fetch();

    }

    public function account()
    {
        $user_id = input('user_id', '');
        if ($this->request->isGet()) {

            if (!$user_id) {
                $this->error('请先登录', 'login/index');
            }
            $user = $this->user->getOne(['user_id' => $user_id]);
            $this->assign('user', $user);
            return $this->fetch();
        } else {
            $data = $this->request->post();
            $user_name = $data['user_name'];
//            $now_pwd=$data['now_pwd'];
//            $user_pwd=$data['user_pwd'];
//            $confirm_pwd=$data['confirm_pwd'];
            if (empty($user_name)) {
                $result = [
                    'status' => 0,
                    'msg' => '提交的数据不合法'
                ];
                return json($result);
            }
            $res = $this->user->saveData(['user_id' => $user_id], $data);
            if ($res) {
                return json([
                    'status' => 1,
                    'msg' => '修改个人信息成功'
                ]);
            } else {
                return json([
                    'status' => 0,
                    'msg' => '修改个人信息失败'
                ]);
            }
            /*
            $node=$this->user->getOne(['user_id'=>$user_id]);

            $salt = getRandKey();
            $salt_now_pwd=md5(md5($now_pwd).$node['salt']);
            if (is_null($node) == false && $node['user_pwd'] == $salt_now_pwd) {
                if($user_pwd==$confirm_pwd){
                    $salt_user_pwd=md5(md5($now_pwd).md5($salt));
                    $data['salt']=$salt;
                    $data['user_pwd']=$salt_now_pwd;
                    unset($data['now_pwd']);
                    $res=$this->user->addData($data);
                    if($res){
                        return json([
                            'status'=>1,
                            'msg' => '修改个人信息成功'
                        ]);
                    }else{
                        return json([
                            'status'=>0,
                            'msg' => '修改个人信息失败'
                        ]);
                    }
                }else{
                    return json([
                        'status'=>0,
                        'msg' => '两次密码不正确'
                    ]);
                }


            } else {
                return json([
                    'status'=>0,
                    'msg' => '用户名或密码错误'
                ]);
            }
            $salt_user_pwd=md5(md5($user_pwd).md5($salt));*/
        }

    }

    public function txImg()
    {
        $id = input('user_id');


        $res = $this->user->saveData(['user_id' => $id], $data);
        if ($res) {
            $this->success('上传头像成功', 'user/account?user_id=' . $id);

        } else {
            $this->error('上传头像失败', 'user/account?user_id=' . $id);
        }
    }

    public function pwdXg()
    {
        $id = input('user_id');
        $data = $this->request->post();
        $now_pwd = $data['now_pwd'];
        $user_pwd = $data['user_pwd'];
        $confirm_pwd = $data['confirm_pwd'];
        if (empty($now_pwd)||empty($user_pwd)||empty($confirm_pwd)) {
            $result = [
                'status' => 0,
                'msg' => '提交的数据不合法'
            ];
            return json($result);
        }
        $node=$this->user->getOne(['user_id'=>$id]);

        $salt = getRandKey();
        $salt_now_pwd=md5(md5($now_pwd).$node['salt']);
//        echo $salt_now_pwd;
//        die;
        if (is_null($node) == false && $node['user_pwd'] ==$salt_now_pwd) {
            if($user_pwd==$confirm_pwd){
                $salt_user_pwd=md5(md5($user_pwd).$salt);
                $data['salt']=$salt;
                $data['user_pwd']=$salt_user_pwd;
                unset($data['now_pwd']);
                unset($data['confirm_pwd']);
                $res = $this->user->saveData(['user_id' => $id], $data);
                if($res){
                    return json([
                        'status'=>1,
                        'msg' => '修改密码成功'
                    ]);
                }else{
                    return json([
                        'status'=>0,
                        'msg' => '修改密码失败'
                    ]);
                }
            }else{
                return json([
                    'status'=>0,
                    'msg' => '两次密码不正确'
                ]);
            }
        } else {
            return json([
                'status'=>0,
                'msg' => '您的当前密码不正确'
            ]);
        }

    }

    public function uploadImg()
    {

        $file = $this->request->file('file_data');
        //原图保存路径
        $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'user';
        $info = $file->move($path);
        //缩略图名称
        $thumb_name = 'thumb_' . $info->getFilename();
        //缩略图保存路径和文件名称
        $thumb_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'user ' . DS . 'thumb' . DS . $thumb_name;
        if ($info) {
            //缩略图
            $image = Image::open($path.DS.$info->getSaveName());
            $image->thumb(200, 200, Image::THUMB_CENTER)->save($thumb_path);
            return json([
                'status' => 1,
                'path' => $info->getSaveName(),
                'thumb' => 'thumb' . DS . $thumb_name
            ]);
        } else {
            return json([
                'status' => 0,
                'err' => $file->getError()
            ]);
        }
    }
    public function logon(){
        Session::clear();
        session_unset();
        $this->redirect("login/index");
    }
}